/*
 * @Author: liao.mingjin@byd.com
 * @Date: 2022-11-18 20:51:40
 * @LastEditors: liao.mingjin@byd.com
 * @LastEditTime: 2022-11-19 00:19:17
 * @FilePath: /project/gitee.com/project_-ccplus/ringbuffer_shm/ringbuffer.h
 * @Description: ring buffer
 * 
 * Copyright (c) 2022 by cal.liao/BYD, All Rights Reserved. 
 */
#ifndef __RINGBUFFER_H
#define	__RINGBUFFER_H

#define min(x,y) ({ 			\
		typeof(x) _x = (x);	\
		typeof(y) _y = (y);	\
		(void) (&_x == &_y);	\
		_x < _y ? _x : _y; })

struct ringbuffer {
	void *data;
	unsigned int size;
	unsigned int read_pos;
	unsigned int write_pos;
};

//struct ringbuffer *ringbuffer_create(unsigned int bytes);
struct ring_buffer_root;
struct ring_buffer_data;

struct ringbuffer *ringbuffer_create(struct ring_buffer_root* rb_root, struct ring_buffer_data* rb_data);
void ringbuffer_destroy(struct ringbuffer *ring_buf);

void ringbuffer_reset(struct ringbuffer *ring_buf);

unsigned int ringbuffer_put(struct ringbuffer *ring_buf, 
 	const char *buf, unsigned int len);

unsigned int ringbuffer_get(struct ringbuffer *ring_buf,
 		char *buf, unsigned int len);

void ringbuffer_print(struct ringbuffer *ring_buf, unsigned int cnt);

ssize_t ringbuffer_from_dev(int fd, struct ringbuffer *ring_buf, unsigned int len);

unsigned int ringbuffer_is_empty(struct ringbuffer *ring_buf);
unsigned int ringbuffer_is_full(struct ringbuffer *ring_buf);

unsigned int ringbuffer_len(struct ringbuffer *ring_buf);
unsigned int ringbuffer_space_left(struct ringbuffer *ring_buf);

unsigned int ringbuffer_to_socket(int socket_fd, struct ringbuffer *ring_buf, unsigned int len);


//=======================================================================
#include<sys/ipc.h>
#include<sys/shm.h>

#define SHM_RBROOT_KEY 0x12345678
#define SHM_RBDATA_KEY 0x87654321

#define SIZE_SHM_RBDATA 1024*1024*4

#define RET_SUCCESS 0
#define RET_FAILTURE -1

//ringbuff struct 
typedef struct ring_buffer_root
{
    //void (*pInitRingbufferRoot)(struct ring_buffer_root* rb_root);
    int (*pCreateRingbufferRoot)();
    //int (*pGetShmRingbufferRoot)(struct ringbuffer* rb_root);
    void* (*pGetShmAddressRingbufferRoot)(struct ring_buffer_root* rb_root);
    int  (*pDeleteRingbufferRoot)(struct ring_buffer_root* rb_root);

    int nSize;
    int nShmid;
	void* pAddress;
}ring_buffer_root;

void InitRingbufferRoot(struct ring_buffer_root* rb_root);
int CreateRingbufferRoot();
void* GetShmAddressRingbufferRoot(struct ring_buffer_root* rb_root);
int DeleteRingbufferRoot(struct ring_buffer_root* rb_root);

//ringbuff's data 
typedef struct ring_buffer_data
{
    //void (*pInitRingbufferData)(struct ring_buffer_data* rb_data);
    int (*pCreateRingbufferData)();
    //int (*pGetShmRingbufferData)(struct ringbuffer* rb_data);
    void* (*pGetShmAddressRingbufferData)(struct ring_buffer_data* rb_data);
    int (*pDeleteRingbufferData)(struct ring_buffer_data* rb_data);
    int nSize;
    int nShmid;
	void* pAddress;
}ring_buffer_data;

void InitRingbufferData(struct ring_buffer_data* rb_data);
int CreateRingbufferData();
void* GetShmAddressRingbufferData(struct ring_buffer_data* rb_data);
int DeleteRingbufferData(struct ring_buffer_data* rb_data);

#endif